package com.hillky.channelhandler.handler;


import com.hillky.YrpcBootstrap;
import com.hillky.enumeration.RespCode;
import com.hillky.exceptions.ResponseException;
import com.hillky.loadbalancer.LoadBalancer;
import com.hillky.protection.CircuitBreaker;
import com.hillky.transport.message.YrpcRequest;
import com.hillky.transport.message.YrpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.SocketAddress;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * 这是一个用来测试的类
 */
@Slf4j
public class MySimpleChannelInboundHandler extends SimpleChannelInboundHandler<YrpcResponse> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, YrpcResponse yrpcResponse) throws Exception {
        //从全局的挂起的请求中寻找匹配的待处理的cf
        CompletableFuture<Object> completableFuture = YrpcBootstrap.PENDING_REQUEST.get(yrpcResponse.getRequestId());

        SocketAddress socketAddress = channelHandlerContext.channel().remoteAddress();
        Map<SocketAddress, CircuitBreaker> everyIpCircuitBreaker = YrpcBootstrap.getInstance().getConfiguration().getEveryIpCircuitBreaker();
        CircuitBreaker circuitBreaker = everyIpCircuitBreaker.get(socketAddress);

        byte code = yrpcResponse.getCode();
        if(code==RespCode.FAIL.getCode()){
            circuitBreaker.recordErrorRequest();
            completableFuture.complete(null);
            log.error("当前id为【{}】的请求，返回错误的结果，响应码[{}]",yrpcResponse.getRequestId(),yrpcResponse.getCode());
            throw new ResponseException(code,RespCode.FAIL.getDesc());
        }else if(code==RespCode.RATE_LIMIT.getCode()){
            circuitBreaker.recordErrorRequest();
            completableFuture.complete(null);
            log.error("当前id为【{}】的请求，被限流，响应码[{}]",yrpcResponse.getRequestId(),yrpcResponse.getCode());
            throw new ResponseException(code,RespCode.RATE_LIMIT.getDesc());
        }else if(code==RespCode.RESOURCE_NOT_FOUND.getCode()){
            circuitBreaker.recordErrorRequest();
            completableFuture.complete(null);
            log.error("当前id为【{}】的请求，资源找不到，响应码[{}]",yrpcResponse.getRequestId(),yrpcResponse.getCode());
            throw new ResponseException(code,RespCode.RESOURCE_NOT_FOUND.getDesc());
        }else if(code==RespCode.SUCCESS.getCode()){
            //服务提供方给予的结果
            Object returnValue = yrpcResponse.getBody();

            // todo 需要针对code做处理
            returnValue=returnValue==null?new Object():returnValue;

            completableFuture.complete(returnValue);

            if(log.isDebugEnabled()){
                log.debug("已寻找到编号为【{}】的completableFuture，处理响应结果",yrpcResponse.getRequestId());
            }
        }else if(code==RespCode.SUCCESS_HEART_BEAT.getCode()){
            completableFuture.complete(null);
            if(log.isDebugEnabled()){
                log.debug("已寻找到编号为【{}】的completableFuture，处理心跳检测，处理响应结果",yrpcResponse.getRequestId());
            }
        }else if(code==RespCode.CLOSING.getCode()){
            completableFuture.complete(null);
            if(log.isDebugEnabled()){
                log.debug("当前id为【{}】的请求，访问被拒绝，目标服务器正处于关闭中，响应码[{}]",yrpcResponse.getRequestId(),yrpcResponse.getCode());
            }

            // 修正负载均衡器
            // 1从健康列表移除
            YrpcBootstrap.CHANNEL_CACHE.remove(channelHandlerContext.channel().remoteAddress());
            // 找到负载均衡器进行reloadBalance 重新进行负载均衡
            LoadBalancer loadBalancer = YrpcBootstrap.getInstance().getConfiguration().getLoadBalancer();
            YrpcRequest yrpcRequest = YrpcBootstrap.REQUEST_THREAD_LOCAL.get();
            loadBalancer.reLoadBalance(yrpcRequest.getRequestPayload().getInterfaceName(),YrpcBootstrap.CHANNEL_CACHE.keySet().stream().toList());

            throw new ResponseException(code,RespCode.RESOURCE_NOT_FOUND.getDesc());
        }


    }
}
